//
//  EVAlertView.m
//  EVClub
//
//  Created by sam on 14/10/27.
//  Copyright (c) 2014年 BitRice. All rights reserved.
//
#import "Masonry.h"
#import "UIView+EV.h"
#import "NSString+EV.h"
#import "EVAlertView.h"
#import <objc/message.h>
static NSUInteger _shownAlertViewCount = 0U; // 当前AlertView的计数器
@interface EVAlertView() {
    NSString *_title;
    NSString *_cancelButton;
    NSArray  *_otherButtons;
    
}
@property (nonatomic, copy) void(^ dismissBlock)(EVAlertViewStruct result);
@property (nonatomic, copy) void(^ completeBlock)();

/**
 *  利用该属性避免EVAlertView的dealloc，一直到TDAlertView dismiss后才dealloc
 */
@property (nonatomic, strong) EVAlertView *retainSelf;

@end

const NSInteger EVAlertViewButtonIndexCancel = 100;
const NSInteger EVAlertViewButtonIndexCommit = 101;

@implementation EVAlertView

- (id)initWithContentView:(UIView *)contentView CompletionBlock:(void (^)())completion dismissBlock:(void (^)(EVAlertViewStruct))dimissBlock
{
    if (self = [super init])
    {
        self.contentView    = contentView;
        self.dismissBlock   = dimissBlock;
        self.completeBlock  = completion;
        // 设置contentView 的 delegate
        if ([contentView respondsToSelector:@selector(setDelegate:)]) {
            ((void(*)(id, SEL, id))objc_msgSend)(contentView, @selector(setDelegate:), self);
        }
    }
    return self;
}

- (id)initWithIcon:(EVAlertIcon)alertIcon title:(NSString *)title detail:(NSString *)detail commitButton:(NSString *)commitButton cancelButton:(NSString *)cancelButton completionBlock:(void (^)())completion dismissBlock:(void (^)(EVAlertViewStruct result))dismissBlock {
    
    return [self initWithTitle:title detail:detail commitButton:commitButton cancelButton:cancelButton dismissBlock:dismissBlock];
    
//    //去掉以前warning 的类型
//    if (alertIcon == EVAlertIconWarning) {
//        alertIcon = EVAlertIconNone;
//        if (isEmpty(title)) {
//            title = @"提示";
//        }
//    }
//
//    EVDefaultAlertConentView *contentView = [[EVDefaultAlertConentView alloc]initWithIcon:alertIcon title:title detail:detail commitButton:commitButton cancelButton:cancelButton];
//    contentView.delegate = self;
//    return [self initWithContentView:contentView CompletionBlock:completion dismissBlock:dismissBlock];
}
+(EVAlertView*)showAlertViewWithMessage:(NSString*)message{
    return [self showAlertViewWithTitle:nil Message:message CommitButton:@"我知道了"];
}
+(EVAlertView*)showAlertViewWithTitle:(NSString*)title Message:(NSString*)message{
    return [self showAlertViewWithTitle:title Message:message CommitButton:@"我知道了"];
}
+(EVAlertView*)showAlertViewWithTitle:(NSString*)title Message:(NSString*)message CommitButton:(NSString*)commitButton{
    EVAlertView *alertView = [[EVAlertView alloc] initWithTitle:title detail:message commitButton:commitButton cancelButton:nil dismissBlock:nil];
    [alertView show];
    return alertView;
}
- (id)initWithTitle:(NSString *)title detail:(NSString *)detail commitButton:(NSString *)commitText cancelButton:(NSString *)cancelText dismissBlock:(void (^)(EVAlertViewStruct result))dismissBlock{
    EVSimpleAlertContentView *contentView = [EVSimpleAlertContentView instanceWithTitle:title detail:detail commitButton:commitText cancelButton:cancelText];
    EVAlertView *alertView = [self initWithContentView:contentView CompletionBlock:nil dismissBlock:dismissBlock];
    contentView.translatesAutoresizingMaskIntoConstraints = NO;
    [contentView mas_updateConstraints:^(MASConstraintMaker *make) {
        if(SCREEN_WIDTH <= 320.0) make.width.equalTo(@(SCREEN_WIDTH - 40));
        else make.width.equalTo(@(SCREEN_WIDTH * 0.7));
        
    }];
    alertView.contentDidAddedBlock = ^{
        [contentView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(@0);
        }];
        [contentView ev_sizeToFitAutoLayout];
    };
    return alertView;
}
- (id)initInputAlertViewWithTitle:(NSString *)title commitButton:(NSString *)commitText cancelButton:(NSString *)cancelText dismissBlock:(void (^)(EVAlertViewStruct result,NSString *inputText))dismissBlock{
    WEAKSELF_DEFINE
    
    EVSimpleAlertContentView *contentView = [EVSimpleAlertContentView instanceInputViewWithTitle:title commitButton:commitText cancelButton:cancelText];
    EVAlertView *alertView = [self initWithContentView:contentView CompletionBlock:nil dismissBlock:^(EVAlertViewStruct result) {
        EVSimpleAlertContentView *contentView = (EVSimpleAlertContentView*)weakSelf.contentView;
        if(dismissBlock) dismissBlock(result,contentView.inputTF.text.ev_trim);
    }];
    alertView.outsideClickedDismiss = YES;
    contentView.translatesAutoresizingMaskIntoConstraints = NO;
    [contentView mas_updateConstraints:^(MASConstraintMaker *make) {
        if(SCREEN_WIDTH <= 320.0) make.width.equalTo(@(SCREEN_WIDTH - 40));
        else make.width.equalTo(@(SCREEN_WIDTH * 0.7));
        
    }];
    alertView.contentDidAddedBlock = ^{
        [contentView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.top.equalTo(@0);
        }];
        [contentView ev_sizeToFitAutoLayout];
    };
    return alertView;
}
- (void)dealloc
{
    self.completeBlock = nil;
    self.dismissBlock  = nil;
}

- (void)setbackgroundColor:(UIColor *)color
{
	self.alertView.backgroundColor = color;
}

- (void)setShowCloseButton:(BOOL)showCloseButton {
    if ([self.contentView isKindOfClass:[EVDefaultAlertConentView class]]) {
        ((EVDefaultAlertConentView *)self.contentView).showCloseButton = showCloseButton;
    }
}

- (BOOL)showCloseButton {
    if ([self.contentView isKindOfClass:[EVDefaultAlertConentView class]]) {
        return ((EVDefaultAlertConentView *)self.contentView).showCloseButton;
    }
    return NO;
}

- (void)show
{
    self.retainSelf = self;
    
    self.alertView = [TDAlertView viewWithContentView:self.contentView cancelButton:nil otherButtons:nil
                                             delegate:self];
    _alertView.contentDidAddedBlock = _contentDidAddedBlock;
    self.alertView.hideDefaultBackground = self.hideDefaultBackground;
    self.alertView.maskColor = self.maskColor;
    self.alertView.layer.cornerRadius = 4.f;
    self.alertView.layer.masksToBounds= YES;
    
    [self.alertView show];
	
    if (self.completeBlock)
    {
        self.completeBlock();
    }
}

- (void)showInView:(UIView *)view {
    self.retainSelf = self;
    
    self.alertView = [TDAlertView viewWithContentView:self.contentView cancelButton:nil otherButtons:nil
                                             delegate:self];
    _alertView.contentDidAddedBlock = _contentDidAddedBlock;
    self.alertView.hideDefaultBackground = self.hideDefaultBackground;
    self.alertView.maskColor = self.maskColor;
    self.alertView.layer.cornerRadius = 4.f;
    self.alertView.layer.masksToBounds= YES;
    
    [self.alertView showInView:view];
    
    if (self.completeBlock)
    {
        self.completeBlock();
    }
}


- (void)dismiss {
    [self.alertView dismiss];
}

- (void)dismissIntemidialy
{
    [self.alertView dismissIntemidialy];
}

- (void)TDAlertView:(TDAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    self.retainSelf = nil;
    
    self.alertView.userInteractionEnabled = NO;
    if (self.alertView.bShowing) {
        [self.alertView dismiss];
        if (self.dismissBlock) {
            EVAlertViewStruct result = (EVAlertViewStruct){self.tag, buttonIndex};
            self.dismissBlock(result);
        }
    }
}

-(void)alertView:(TDAlertView *)alertView didClickOutside:(UIView *)contentView
{
	if (_outsideClickedDismiss) {
		return;
	}
    if (!contentView && self.contentView)
    {
        if (self.alertView.bShowing) {
            [self.alertView dismissIntemidialy];
            if (self.dismissBlock) {
                EVAlertViewStruct result = (EVAlertViewStruct){self.tag, -1};
                self.dismissBlock(result);
            }
        }
    }
}

+ (EVAlertView *)showMessageAlertView:(NSString *)message {
    return [EVAlertView showMessageAlertView:message commitButton:@"我知道了"];
}

+ (EVAlertView *)showMessageAlertView:(NSString *)message detail:(NSString *)detail icon:(EVAlertIcon)icon
{
    return [EVAlertView showMessageAlertView:message detail:detail icon:icon showCloseButton:YES];
}

+ (EVAlertView *)showMessageAlertView:(NSString *)message detail:(NSString *)detail icon:(EVAlertIcon)icon showCloseButton:(BOOL)showCloseButton
{
    EVAlertView *alertView = [EVAlertView showMessageAlertView:message detail:detail icon:icon commitButton:@"我知道了" cancelButton:nil dismissBlock:nil];
    alertView.showCloseButton = showCloseButton;
    return alertView;
}

+ (EVAlertView *)showMessageAlertView:(NSString *)message commitButton:(NSString *)commitButton {
    return [EVAlertView showMessageAlertView:message detail:nil commitButton:commitButton];
}

+ (EVAlertView *)showMessageAlertView:(NSString *)message detail:(NSString *)detail commitButton:(NSString *)commitButton {
    return [self showMessageAlertView:message detail:detail commitButton:commitButton cancelButton:nil dismissBlock:nil];
}

+ (EVAlertView *)showMessageAlertView:(NSString *)message commitButton:(NSString *)commitButton cancelButton:(NSString *)cancelButton dismissBlock:(void (^)(EVAlertViewStruct result))dismissBlock {
    return [self showMessageAlertView:@"提示" detail:message commitButton:commitButton cancelButton:cancelButton dismissBlock:dismissBlock];
}

+ (EVAlertView *)showMessageAlertView:(NSString *)message detail:(NSString *)detail commitButton:(NSString *)commitButton cancelButton:(NSString *)cancelButton dismissBlock:(void (^)(EVAlertViewStruct result))dismissBlock {
    if (detail == nil) {
        detail = message;
        message = @"提示";
    }
    return [self showMessageAlertView:message detail:detail icon:EVAlertIconNone commitButton:commitButton cancelButton:cancelButton dismissBlock:dismissBlock];
}

+ (EVAlertView *)showMessageAlertView:(NSString *)message icon:(EVAlertIcon)alertIcon commitButton:(NSString *)commitButton cancelButton:(NSString *)cancelButton dismissBlock:(void (^)(EVAlertViewStruct result))dismissBlock {
    return [self showMessageAlertView:nil detail:message icon:alertIcon commitButton:commitButton cancelButton:cancelButton dismissBlock:dismissBlock];
}

+ (EVAlertView *)showMessageAlertView:(NSString *)message detail:(NSString *)detail icon:(EVAlertIcon)alertIcon commitButton:(NSString *)commitButton cancelButton:(NSString *)cancelButton dismissBlock:(void (^)(EVAlertViewStruct result))dismissBlock {
    return [self showMessageAlertViewInView:nil title:message detail:detail icon:alertIcon commitButton:commitButton cancelButton:cancelButton dismissBlock:dismissBlock];
}

+ (EVAlertView *)showMessageAlertViewInView:(UIView *)showView title:(NSString *)message detail:(NSString *)detail icon:(EVAlertIcon)alertIcon commitButton:(NSString *)commitButton cancelButton:(NSString *)cancelButton dismissBlock:(void (^)(EVAlertViewStruct result))dismissBlock {
    EVAlertView *alertView = [[EVAlertView alloc]initWithIcon:alertIcon title:message detail:detail commitButton:commitButton cancelButton:cancelButton completionBlock:nil dismissBlock:^(EVAlertViewStruct result) {
        if (dismissBlock != nil) {
            dismissBlock(result);
        }
        _shownAlertViewCount = MAX(0, (int)_shownAlertViewCount - 1);
    }];
    
    if (showView == nil) {
        [alertView show];
    }
    else {
        [alertView showInView:showView];
    }
    
    _shownAlertViewCount ++;
    return alertView;
}

+ (BOOL)existAlertView {
    return _shownAlertViewCount > 0;
}

@end
